package com.example.navigation.fragment

import android.os.Bundle
import android.view.View
import androidx.core.app.ActivityOptionsCompat
import androidx.navigation.ActivityNavigator
import androidx.navigation.NavOptions
import androidx.navigation.findNavController
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.findNavController
import com.example.navigation.R
import com.example.navigation.base.BaseFragment
import kotlinx.android.synthetic.*
import kotlinx.android.synthetic.main.fragment_login.*
import kotlinx.android.synthetic.main.fragment_login.view.*

/**
 * @author Liyg
 * @description: 登陆页面
 * @date :2020/11/19
 */
class LoginFragment : BaseFragment() {

    /**
     * 布局资源
     */
    override fun getLayoutResId(): Int {
        return R.layout.fragment_login;
    }

    override fun initView(rootView: View) {
        super.initView(rootView)

        rootView.toRegisterPage.setOnClickListener {
            //跳转到注册页面,Fragment之间的元素共享
            /*
             第一种共享元素的方法

             val extras = FragmentNavigatorExtras(userAvatarIv to "userAvatarTn")
             */


            val imagePair = Pair<View, String>(userAvatarIv, "userAvatarTn")
            val userNamePair = Pair<View, String>(userNameEt, "userNameTn")
            val extras = FragmentNavigatorExtras(imagePair, userNamePair)

            //数据传参
            val bundle = Bundle()
            bundle.putString("userName", userNameEt.text.toString())

            findNavController().navigate(
                R.id.to_register_fragment,
                bundle,
                null,
                extras
            )
        }

        rootView.toForgetPage.setOnClickListener {
            //跳转到忘记密码页面
            findNavController().navigate(R.id.to_forget_fragemnt)
        }

        //跳转到Activity
        rootView.toAgreementPage.setOnClickListener {
            //跳转到用户协议页面
            //Activity之间的共享元素
            val pair = androidx.core.util.Pair<View, String>(userAvatarIv, "userAvatarTn")
            val userNamePair = androidx.core.util.Pair<View, String>(userNameEt, "userNameTn")

            val options =
                ActivityOptionsCompat.makeSceneTransitionAnimation(requireActivity(), pair,userNamePair)

            val extras = ActivityNavigator.Extras.Builder()
                .setActivityOptions(options)
                .build()

            //数据传参
            val bundle = Bundle()
            bundle.putString("userName", userNameEt.text.toString())

            findNavController().navigate(
                R.id.to_agreement_activity
                , bundle
                , null
                , extras
            )
        }
    }
}